package com.zrrd.gmall.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.zrrd.gmall.item.service.ItemService;
import com.zrrd.gmall.list.feign.ListFeignClient;
import com.zrrd.gmall.model.product.BaseCategoryView;
import com.zrrd.gmall.model.product.SkuInfo;
import com.zrrd.gmall.model.product.SpuSaleAttr;
import com.zrrd.gmall.product.fegin.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@Service
public class ItemServiceImpl implements ItemService {
    @Autowired
    private ProductFeignClient productFeignClient;
    @Autowired
    private ListFeignClient listFeignClient;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;
    @Override
    public Map<String, Object> getBySkuId(Long skuId) {
        Map<String, Object> map = new HashMap<>();
        //1基本信息

        CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            map.put("skuInfo", skuInfo);
            return skuInfo;
        },threadPoolExecutor);


        //2.通过feign拿三级分类id
        CompletableFuture<Void> categoryViewCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            BaseCategoryView baseCategoryView = productFeignClient.getBaseCategoryView(skuInfo.getCategory3Id());
            map.put("categoryView", baseCategoryView);
        },threadPoolExecutor);


        //3获取sku的价格
        CompletableFuture<Void> priceCompletableFuture = CompletableFuture.runAsync(new Runnable() {
            @Override
            public void run() {
                BigDecimal price = productFeignClient.getSkuPrice(skuId);
                map.put("price", price);
            }
        }, threadPoolExecutor);


        //4 根据SkuId和spuId 查询出sku对应的spu 的销售属性以及属性值以及标记当前sku所选中的销售属性
        CompletableFuture<Void> spuSaleAttrListCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            List<SpuSaleAttr> skuSaleAttrListBySkuId = productFeignClient.getSkuSaleAttrListBySkuId(skuId, skuInfo.getSpuId());
            map.put("spuSaleAttrList", skuSaleAttrListBySkuId);
        },threadPoolExecutor);

        //5.获取切换的数据
        CompletableFuture<Void> valueSkuJsoncompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            Map skuValueIdsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
            String mapJson = JSON.toJSONString(skuValueIdsMap);
            map.put("valuesSkuJson",mapJson);
        }, threadPoolExecutor);

        //6.更新商品热度的服务
        CompletableFuture<Void> incrHotScoreCompletableFuture = CompletableFuture.runAsync(() -> {
            listFeignClient.incrHotScore(skuId);
        }, threadPoolExecutor);


        //组合任务
        CompletableFuture.allOf(
                skuInfoCompletableFuture,
                categoryViewCompletableFuture,
                priceCompletableFuture,
                spuSaleAttrListCompletableFuture,
                valueSkuJsoncompletableFuture,
                incrHotScoreCompletableFuture
        ).join();

        System.out.println("汇总的Map======>"+map);
        return map;
    }
}
